package com.example.test.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.example.test.service.config.RsaFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSet;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

@Service
public class EncodeServiceImpl {

    public void rsa() throws Exception {

        KeyPair pair = SecureUtil.generateKeyPair("RSA");

        String encode = Base64.encode(pair.getPrivate().getEncoded());
        System.out.println(encode);
        String encode1 = Base64.encode(pair.getPublic().getEncoded());
        System.out.println(encode1);

        AsymmetricCrypto asymmetricCrypto = new AsymmetricCrypto("RSA", getRSAPrivateKeyBybase64(encode),
                getRSAPublidKeyBybase64(encode1));

        String s1 = asymmetricCrypto.encryptBase64("你好呀", KeyType.PublicKey);
        System.out.println(s1);

        byte[] s = asymmetricCrypto.decrypt(s1, KeyType.PrivateKey);
        System.out.println(new String(s));

    }

    @Autowired
    RsaFactory rsaFactory;


    public void password() {

        // 原始密码 公钥加密 base64加密传到后端

        String mm = "123456";

        RSA rsa = rsaFactory.getinstance();

        byte[] encrypt = rsa.encrypt(StrUtil.bytes(mm, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);

        System.out.println(Base64.encode(encrypt));

        // ------------------前后端分界线----------------


        // 后端 base64解密 rsa 解密得到原始密码
        byte[] decode = Base64.decode(Base64.encode(encrypt));

        byte[] decrypt = rsa.decrypt(decode, KeyType.PrivateKey);

        String orgmm = new String(decrypt);
        System.out.println("原始密码"+orgmm);

        // 原始密码加随机盐存储到数据  并加上 随机盐一并存储
        String x = RandomUtil.randomString(4);
        System.out.println(SecureUtil.md5(orgmm+x));

    }



    /**
     * @desc: 将字符串转换成RSAPublicKey类型
     * @date 2020-6-12 11:03:05
     * @param
     * @return
     */
    public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) throws Exception {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec((new BASE64Decoder()).decodeBuffer(base64s));
        RSAPublicKey publicKey = null;
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        try {
            publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
        } catch (InvalidKeySpecException var4) {

        }
        return publicKey;
    }

    /**
     * @desc: 将字符串转换成RSAPrivateKey类型
     * @date 2020-6-12 11:03:01
     * @param
     * @return
     */
    public static RSAPrivateKey getRSAPrivateKeyBybase64(String base64s) throws Exception{
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec((new BASE64Decoder()).decodeBuffer(base64s));
        RSAPrivateKey privateKey = null;
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        try {
            privateKey = (RSAPrivateKey)keyFactory.generatePrivate(keySpec);
        } catch (InvalidKeySpecException var4) {
        }
        System.out.println(privateKey);
        return privateKey;
    }

}
